package com.cblz.bd.server.frontend.controller;

import cn.hutool.core.bean.BeanUtil;
import com.cblz.bd.common.core.constant.ResponseStatusCode;
import com.cblz.bd.common.core.exception.BdAdminException;
import com.cblz.bd.common.core.exception.ValidateCodeException;
import com.cblz.bd.common.core.utils.ResultResponse;
import com.cblz.bd.server.core.annotation.UserApiController;
import com.cblz.bd.server.frontend.entity.BaUserRuleTree;
import com.cblz.bd.server.frontend.entity.vo.BaUserLoginRegisterVO;
import com.cblz.bd.server.frontend.service.BaUserRuleService;
import com.cblz.bd.server.frontend.service.BaUserService;
import com.cblz.bd.server.utils.StpUserUtil;
import io.vavr.control.Option;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * {@code @Name:} BaUserController
 * {@code @Description: }
 * {@code @Author:} by yanjieping
 * {@code @CreateDate:} 2023/2/23 - 周四
 */
@UserApiController("api/user")
@RequiredArgsConstructor
public class BaUserController {

    private final BaUserService baUserService;
    private final BaUserRuleService baUserRuleService;

    /**
     * 登录前准备工作
     *
     * @return
     */
    @GetMapping("checkIn")
    public ResultResponse checkIn() {
        return new ResultResponse().data(Map.of("accountVerificationType", Collections.emptyList()));
    }

    /**
     * 登录
     *
     * @param loginRegisterVO
     * @return
     */
    @PostMapping("checkIn")
    public ResultResponse checkIn(@RequestBody BaUserLoginRegisterVO loginRegisterVO) {
        try {
            Map<String, Object> loginResult = baUserService.login(loginRegisterVO);
            if (!loginResult.isEmpty())
                return new ResultResponse().data(loginResult);
            return new ResultResponse().message(ResponseStatusCode.ERROR.getStatusCode(), "用户名和密码错误");
        } catch (ValidateCodeException e) {
            throw new BdAdminException(e.getMessage());
        }
    }

    /**
     * 进入用户业的前置查询
     *
     * @return
     */
    @GetMapping("index")
    public ResultResponse index() {
        BaUserLoginRegisterVO baUserLoginRegisterVO = BeanUtil.copyProperties(Option.of(baUserService.getById(Option.of(StpUserUtil.getLoginId().toString()).getOrElseThrow(() -> {
            throw new BdAdminException("登录信息失效,请重新登录");
        }))).getOrElseThrow(() -> {
            throw new BdAdminException("未找到ID为「" + StpUserUtil.getLoginId() + "」的用户");
        }), BaUserLoginRegisterVO.class);

        baUserLoginRegisterVO.setPassword("");
        baUserLoginRegisterVO.setToken(StpUserUtil.getTokenValueByLoginId(StpUserUtil.getLoginId()));

        List<BaUserRuleTree> baUserRuleTrees = baUserRuleService.getAllUserRule(StpUserUtil.getLoginId().toString());

        return new ResultResponse().data(Map.of("userInfo", baUserLoginRegisterVO, "menus", baUserRuleTrees));
    }

    @PostMapping("logout")
    public ResultResponse logout() {
        StpUserUtil.logout();
        return new ResultResponse().message("注销成功");
    }
}
